// 封装翻译API
// 获取常见四六级词汇列表
export const translateList = (data={})=>{
	return new Promise((resolve,reject)=>{
		uni.request({
			url:"https://cdn.jsdelivr.net/gh/lyc8503/baicizhan-word-meaning-API/data/list.json",
			data:data,
			method:"GET",
			success(res) {
				resolve(res.data)
			},
			fail(err){
				reject(err)
			}
		})
	})
}
// 用于获取词汇词义（翻译）
export const translateWord = (word="hello")=>{
	return new Promise((resolve,reject)=>{
		uni.request({
			url:`https://cdn.jsdelivr.net/gh/lyc8503/baicizhan-word-meaning-API/data/words/${word}.json`,
			// url:`http://www.youdao.com/w/${word}/#keyfrom=dict2.top`,
			method:"GET",
			timeout:5000,
			success(res) {
				resolve(res.data)
			},
			fail(err){
				reject(err)
			}
		})
	})
}

// 翻译发音
let innerAudioContext = null;
export const translateSound = (word)=> {
	// 在播放音频之前 重置音频上下文对象
	if (innerAudioContext) {
	  try {
	    innerAudioContext.pause();
	    innerAudioContext.destroy();
	    innerAudioContext = null;
		 console.log('停止/暂停');
	  } catch (e) {
	    //TODO handle the exception
	  }
	}
	// 创建音频上下文对象
	innerAudioContext = uni.createInnerAudioContext();
	// 自动播放
	innerAudioContext.autoplay = true;
	// 设置音频路径
	// innerAudioContext.src = `https://fanyi.baidu.com/gettts?lan=uk&text=${word}&spd=3&source=web`;
	innerAudioContext.src = `https://dict.youdao.com/dictvoice?audio=${word}&type=2`;
	innerAudioContext.onPlay(() => {
	  console.log('翻译..');
	});
	innerAudioContext.onError((res) => {
	  console.log(res.errMsg);
	  console.log(res.errCode);
	});
}